﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using XNAFight.Scene.Core;
using XNAFight.Engine;
using XNAFight.Engine.Inputs;
using System;
using System.Diagnostics;

namespace XNAFight.Scene.SandBoxOne
{
    class SandBoxOne : Battlefield
    {
        #region CHAMPS

        private bool paused;
        private SpecificMenuInputInterpreter menu_interpreter;

        private List<GameObject> gameObjects;

        private SandBoxOneMenu menu;
        private DebugScreenOne debug_one;
        private DebugMouse debug_mouse;

        #endregion

        #region PROPRIETES

        public List<GameObject> GameObjects
        {
            get { return gameObjects; }
            
        }

        #endregion

        public SandBoxOne(string _nom_perso_p_one, string _nom_perso_p_two, Controler _controler_p_one, Controler _controler_p_two, InGameInputConfiguration _inputConf_p_one, InGameInputConfiguration _inputConf_p_two)
            : base(350, _nom_perso_p_one, _nom_perso_p_two, _controler_p_one, _controler_p_two, _inputConf_p_one, _inputConf_p_two)
        {
            paused = false;

            gameObjects = new List<GameObject>();   // La liste de tous les gameobjects (qui vont être updatés et affichés du coup)

            menu_interpreter = new SpecificMenuInputInterpreter(PlayerOne.Control);

            gameObjects.Add(PlayerOne.Personnage);
            gameObjects.Add(PlayerTwo.Personnage);

            menu = new SandBoxOneMenu("Pause");
            menu.Adding += pause;
            menu.Cancelled += unpause;

            menu.Completed += leaveInGame;

            debug_one = new DebugScreenOne(PlayerOne);
            debug_mouse = new DebugMouse();

            this.Added += this.Activate;
            this.Removed += debug_one.Complete;
            this.Removed += debug_mouse.Complete;
            this.Removed += this.Disable;
        }
        
        protected override void LoadContent()
        {
            base.LoadContent();

            // Chargement de tous les gameobjects, s'ils ont une texture_name différente de null et s'ils sont dans la liste des gamesObjects
            foreach (GameObject _gameObject in gameObjects)
            {
                _gameObject.Load();
            }
        }

        public override void Update(GameTime _game_time)
        {
            if (paused)
                return;
            
            base.Update(_game_time);

            float elapsed = (float)_game_time.ElapsedGameTime.TotalSeconds;

            foreach (GameObject _gameObject in gameObjects)
            {
                _gameObject.Update(elapsed);
            }
        }
        
        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            CommonContent.SpriteBatch.Begin();

            // La boucle qui va afficher tous les éléments du jeu (à insérer lors de leur création dans la liste GameObjects
            foreach (GameObject _gameObject in GameObjects)
            {
                if (_gameObject.Alive)
                {
                    _gameObject.draw(CommonContent.SpriteBatch);                    
                }
            }

            CommonContent.SpriteBatch.End();
        }

        public void pause()
        {
            paused = true;
        }
        public void pause(object _sender, EventArgs _e)
        {
            pause();
        }
        public void unpause()
        {
            paused = false;
        }
        public void unpause(object _sender, EventArgs _e)
        {
            unpause();
        }

        public override void Reset()
        {
        }

        public override void HandleInputs()
        {
            if (menu_interpreter.isMenuCanceled())
            {
                menu.Add();
            }
            if (menu_interpreter.isDebugPressed())
            {
                if (debug_one.IsAlive)
                    debug_one.Cancel();
                else
                    debug_one.Add();

                if (debug_mouse.IsAlive)
                    debug_mouse.Cancel();
                else
                    debug_mouse.Add();
            }
        }

        private void leaveInGame()
        {
            Game game = this.Game;
            complete = true;
            Remove();
            Program.CreateMainMenu();
        }
        private void leaveInGame(object _sender, EventArgs _e)
        {
            leaveInGame();
        }
    }
}
